/** BCMI_Frame Log Recorder Tool
 *
 *  BCMI_Frame Log Recorder Tool
 *  Define the Log how to record.
 *
 *  @author : huicong
 *  @date : 2008-10-28
 *
 */

#include "LogRecorder.h"

#include <cstring>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <sstream>
using namespace std;

namespace bcmi_frame
{

    LogRecorder::LogRecorder(IN CSTRING _fileName, FrameClock *_clock)
        :_compressSave(FALSE)
    {
        //UNDO parameter check
        _logFile = fopen(_fileName, "wb");
        fprintf(_logFile, "----Log Start----");
        //UNDO check _logFile open successful
        _logClock = _clock;
    }

    LogRecorder::~LogRecorder()
    {
        fprintf(_logFile, "\n----Log End----");
        fclose(_logFile);
        _logClock = NULL;
    }

    HRESULT LogRecorder::log(
        IN INT64 _systemTime, 
        IN DOUBLE _mpiTime, 
        IN HRESULT _logStatus, 
        IN bcmi_frame::LogMethod _method, 
        IN CSTRING _logWriter, 
        IN VOID *_logInfo,
        IN UINT _infoLength)
    {
        //UNDO parameter check
        HRESULT hr = H_OK;
        if (TRUE == _compressSave)
        {
            hr = compressLog(
                _systemTime,
                _mpiTime,
                _logStatus,
                _method,
                _logWriter,
                _logInfo,
                _infoLength);
        }
        else 
        {
            hr = uncompressLog(
                _systemTime,
                _mpiTime,
                _logStatus,
                _method,
                _logWriter,
                _logInfo,
                _infoLength);
        }
        //UNDO
        return hr;
    }

    HRESULT LogRecorder::log(const CSTRING _logInfo)
    {
        return log(
            _logClock->getCurrentSystemTime(),
            _logClock->getCurrentMPITime(),
            H_OK,
            bcmi_frame::USER,
            "bcmi_frame",
            _logInfo,
            strlen(_logInfo)
            );
    }

    HRESULT LogRecorder::log(IN CSTRING _logWriter, IN CSTRING _logInfo)
    {
        return log(
            _logClock->getCurrentSystemTime(),
            _logClock->getCurrentMPITime(),
            H_OK,
            bcmi_frame::USER,
            _logWriter,
            _logInfo,
            strlen(_logInfo)
            );
    }

    HRESULT LogRecorder::log(IN HRESULT _logStatus, IN CSTRING _logWriter, IN CSTRING _logInfo)
    {
        return log(
            _logClock->getCurrentSystemTime(),
            _logClock->getCurrentMPITime(),
            _logStatus,
            bcmi_frame::USER,
            _logWriter,
            _logInfo,
            strlen(_logInfo)
            );
    }

    HRESULT LogRecorder::log(
        IN HRESULT _logStatus, 
        IN LogMethod _method, 
        IN CSTRING _logWriter, 
        IN CSTRING _logInfo)
    {
        return log(
            _logClock->getCurrentSystemTime(),
            _logClock->getCurrentMPITime(),
            _logStatus,
            _method,
            _logWriter,
            _logInfo,
            strlen(_logInfo)
            );
    }

    HRESULT LogRecorder::log(
        IN HRESULT _logStatus, 
        IN LogMethod _method, 
        IN CSTRING _logWriter, 
        IN VOID* _logInfo, 
        IN UINT _infoLength)
    {
        return log(
            _logClock->getCurrentSystemTime(),
            _logClock->getCurrentMPITime(),
            _logStatus,
            _method,
            _logWriter,
            _logInfo,
            _infoLength
            );
    }

    VOID LogRecorder::setCompress(IN BOOL _compress)
    {
        fprintf(_logFile, "\n----next %s----", ((_compress) ? "compress" : "uncompress" ));
    }

    HRESULT LogRecorder::compressLog(
        IN INT64 _systemTime,
        IN DOUBLE _mpiTime,
        IN HRESULT _logStatus,
        IN LogMethod _method, 
        IN CSTRING _logWriter,
        IN VOID* _logInfo,
        IN UINT _infoLength)
    {
        HRESULT hr = H_OK;
        //UNDO
        return hr;
    }

    HRESULT LogRecorder::uncompressLog(
        IN INT64 _systemTime,
        IN DOUBLE _mpiTime,
        IN HRESULT _logStatus,
        IN LogMethod _method, 
        IN CSTRING _logWriter,
        IN VOID* _logInfo,
        IN UINT _infoLength)
    {
        HRESULT hr = H_OK;
        //UNDO
        fprintf(
            _logFile,
            "\nST[%lld]\tMT[%lf]\tHR[%d]\tLM[%d]\tLW[%s] : LEN[%d]\"",
            _systemTime, //ST[%lld]
            _mpiTime, //MT[%lf]
            _logStatus, //HR[%d]
            _method, //LM[%d]
            _logWriter, //LW[%s]
            _infoLength //LEN[%d]
            );
        fwrite(((BYTE*)_logInfo), sizeof(BYTE), _infoLength, _logFile);
        fprintf(_logFile, "\"");
        return hr;
    }

    VOID LogRecorder::logFlush()
    {
        fflush(_logFile);
    }
};
